package com.bit.mybatis.mapper;

import com.bit.mybatis.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {

//    @Select("select * from user_info")
//    @Select("select id, username,`password`, age, gender,  phone, " +
//            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime" +
//            " from user_info")

//    @Results(id = "BaseMap", value = {
//            @Result(column = "delete_flag", property = "deleteFlag"),
//            @Result(column = "create_time", property = "createTime"),
//            @Result(column = "update_time", property = "updateTime")
//    })
//    @Select("select * from user_info")

    @Select("select * from user_info")
    List<UserInfo> selectAll();


//    @ResultMap(value = "BaseMap")
    @Select("select * from user_info where id = #{id}")
    UserInfo selectAllById(Integer id);

    @Select("select * from user_info where id = '${id}'")
    UserInfo selectAllById3(Integer id);

    @Select("select * from user_info where id = #{aa}")
    List<UserInfo> selectAllById2(Integer id);

//    @Select("select * from user_info")
//    UserInfo selectAll2();

    @Select("SELECT * FROM `user_info` where username=#{userName} and `password` = #{password} ")
    List<UserInfo> selectByNameAndPassword(@Param("password") String password, @Param("userName") String username);

    @Select("SELECT * FROM `user_info` where username='${userName}' and `password` = '${password}' ")
    List<UserInfo> selectByNameAndPassword2(@Param("password") String password, @Param("userName") String username);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age) VALUES (#{username}, #{password}, #{age})")
    Integer insertUser(UserInfo userInfo);


    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into " +
            "user_info (username, `password`, age) " +
            "VALUES (#{userInfo.username}, #{userInfo.password}, #{userInfo.age})")
    Integer insertUser2(@Param("userInfo") UserInfo userInfo);


    @Delete("delete from user_info where id = #{id}")
    Integer deleteUser(Integer id);

    @Update("update user_info set delete_flag = #{deleteFlag}, phone = #{phone} where id = #{id}")
    Integer updateUser(UserInfo userInfo);


    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectUserInfoByOrder(String order);

    @Select("select * from user_info where username like CONCAT('%',#{userName},'%') ")
    List<UserInfo> selectUserInfoByLike(String userName);

    @Insert("<script>" +
            "insert into user_info " +
            "<trim prefixOverrides=\",\" suffixOverrides=\",\" prefix=\"(\" suffix=\")\">" +
            "<if test='username!=null'> username, </if>" +
            "<if test='password!=null'> password, </if>" +
            "<if test='age!=null'> age, </if>" +
            "<if test='gender!=null'> gender, </if>" +
            "<if test='phone!=null'> phone, </if>" +
            "</trim> VALUES " +
            "<trim prefixOverrides=\",\" suffixOverrides=\",\" prefix=\"(\" suffix=\")\">" +
            "<if test='username!=null'> #{username}, </if>" +
            "<if test='password!=null'> #{password}, </if>" +
            "<if test='age!=null'> #{age}, </if>" +
            "<if test='gender!=null'> #{gender}, </if>" +
            "<if test='phone!=null'> #{phone}, </if>" +
            "</trim>" +
            "</script>")
    Integer insertUser3(UserInfo userInfo);
}
